<?php
/**
 * Fastlib程序安装脚本
 * 
 * $Id: install.php 130 2010-10-10 16:47:37Z Administrator $
 * $Revision: 130 $
 * $Author: Administrator $
 * $Date: 2010-10-10 16:47:37 +0000 (Sun, 10 Oct 2010) $
 * @author glzaboy<glzaboy@163.com>
 * @package fastlib
 * @subpackage misc
 * @copyright 2005-2010 (c) glzaboy
 */
if (! defined ( '__FASTLIB__' )) {
	header ( "HTTP/1.1 301 Moved Permanently" );
	header ( "Last-Modified:" . date ( 'r' ) );
	header ( "Expires: " . date ( 'r', time () + 86400 ) );
	header ( "Location: {$_SERVER['HTTP_REF']}" );
	exit ( "HTTP/1.1 301 Moved Permanently " );
}
if (in_array ( strtolower ( __FASTMVC_ACTV__ ), array ('install', 'remove' ) )) {
	$this->modulesStore ( 'install', __FASTMVC_ACTV__, __FASTMVC_MV__, array ('list', 'save' ), 'list' );
	if (! isset ( $_GET ['silence'] ) or ! $_GET ['silence']) {
		new message ( __ ( "Moduel update sucess!" ), array ("value" => __ ( "back" ), "key" => "b", "url" => $_SERVER ['PHP_SELF'] ), __ ( "Moduel update" ), 0, 1, 0 );
	}
}
global $sitecfg, $flibcfg;
switch (__FASTMVC_ACTV__) {
	case 'list' :
		$d = dir ( 'sources' );
		$dir = array ();
		while ( false !== ($tmpname = $d->read ()) ) {
			if (is_file ( 'sources/' . $tmpname ) && ! in_array ( strtolower ( $tmpname ), array ('install.php', 'install.desc' ) )) {
				array_push ( $dir, substr ( $tmpname, 0, - 4 ) );
			}
		}
		$d->close ();
		$desc = array ();
		if (file_exists ( 'sources/install.desc' )) {
			$desc_tmp = file ( 'sources/install.desc' );
			if (is_array ( $desc_tmp )) {
				foreach ( $desc_tmp as $v ) {
					$split = explode ( '=', $v );
					$desc [trim ( $split [0] )] = trim ( $split [1] );
				}
			}
		}
		$data = new data ();
		if ($_SESSION ['wml'] == 1) {
			FastLoad::loadLib ( 'wml', __FILE__, __LINE__ );
			$g = wmlGroups ();
			$card = wmlCard ();
			wmlSetvar ( $card, 'modules', $dir );
			wmlSetvar ( $card, 'database', $data->loadCfg ( $flibcfg ['database'] ) );
			wmlSetvar ( $card, 'desc', $desc );
			wmlSetvar ( $card, 'timezone', timezoneList ( 1 ) );
			wmlAddcard ( $g, $card, __FASTTPL__ . '/install.fs', array ('title' => 'install application base on fastlib', 'newcontext' => 'true' ) );
			setSitecfg ( 'unicode', true );
			wmlOutput ( $g, null, 0 );
		} else {
			$tpl = new fasttpl ();
			setDocTitle ( 'install application base on fastlib' );
			$tpl->assign ( 'modules', $dir );
			$tpl->assign ( 'database', $data->loadCfg ( $flibcfg ['database'] ) );
			$tpl->assign ( 'timezone', timezoneList ( 0 ) );
			$tpl->assign ( 'desc', $desc );
			unset ( $d, $dir );
			$tpl->display ( __FASTTPL__ . '/install.fs' );
		}
		break;
	case 'save' :
		if (mvc::modulesStore ( null, 'getlock' )) {
			new message ( 'module Lock!' );
			exit ();
		} else {
			$save = array ('database' => array ('Server' => ( string ) $_POST ['Server'], 'user' => ( string ) $_POST ['user'], 'pwd' => ( string ) $_POST ['user'], 'db' => ( string ) $_POST ['db'], 'charset' => ( string ) $_POST ['charset'], 'data_prefix' => ( string ) $_POST ['data_prefix'], 'sql_prefix' => ( string ) $_POST ['sql_prefix'], 'driver' => ( string ) $_POST ['driver'] ), 'siteName' => ( string ) $_POST ['siteName'], 'lang' => ( string ) $_POST ['lang'], 'timezone' => ( string ) $_POST ['timezone'], 'rewriteRule' => ( string ) $_POST ['rewriteRule'] );
			$string = <<<ET
<?php
\$sitecfg['database']=array(
    'Server'=>'{$_POST['Server']}',
    'user'=>'{$_POST['user']}',
    'pwd'=>'{$_POST['pwd']}',
    'db'=>'{$_POST['db']}',
    'charset'=>'{$_POST['charset']}',
    'data_prefix'=>'{$_POST['data_prefix']}',
    'sql_prefix'=>'{$sitecfg['sql_prefix']}',
    'driver'=>'{$_POST['driver']}',
    );
\$sitecfg['siteName']='{$_POST['siteName']}';
\$sitecfg['timezone']='{$_POST['timezone']}';
ET;
			if ($_POST ['rewriteRule'] == '' or $_POST ['rewriteRule'] == null) {
				$string .= PHP_EOL . '$sitecfg[\'rewriteRule\']=null;';
			} else {
				$string .= PHP_EOL . '$sitecfg[\'rewriteRule\']=\'' . ( string ) $_POST ['rewriteRule'] . '\';';
			}
			file_put_contents ( getSiteCurrent ( true ) . 'data/site.setting.php', $string );
			new message ( 'Save Sucess', array ('value' => 'back', 'url' => $this->rewrite ( null, 'list' ) ), 'Config save' );
			exit ();
		}
		break;
}